package cn.xerllent.wxtp.service.mapper;

import java.util.List;

import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import cn.xerllent.config.MybatisRedisCache;
import cn.xerllent.wxtp.entity.WxUser;
import cn.xerllent.wxtp.service.dto.wxUserFilter;


@Repository
@Mapper
@CacheNamespace(implementation = MybatisRedisCache.class, eviction = MybatisRedisCache.class)
public interface WxUserMapper extends BaseMapper<WxUser> {

	
    /**
     * 查询参数包含groupId，需要联表查询 username,truename,nickname,phone,comment,mail
     * */
    @Options(useCache = false)
    @Select("<script>"
    		+ "SELECT u.* FROM `wxtp_user` u "
    		+ "<if test = \"departmentIds!=null and departmentIds.size()>0\" > "
    			+ " left outer join `wxtp_department_user` g on u.user_id=g.user_id and u.corp_id=g.corp_id"
    		+ "</if>"
			+ "<where>"
    		+ "<if test = \"corpId!=null\" > AND u.corp_id = #{corpId} </if>"
    		+ "<if test = \"blurry!=null and blurry!=''\" >"
    		+ " AND ("
	    		+ "u.name LIKE concat('%', #{blurry}, '%') "
	    		+ "or u.telephone LIKE concat('%', #{blurry}, '%') "
	    		+ "or u.alias LIKE concat('%', #{blurry}, '%') "
	    		+ "or u.phone LIKE concat('%', #{blurry}, '%') "
	    		+ "or u.address LIKE concat('%', #{blurry}, '%') "
    		+ ")"
    		+ "</if>"
    		+ "<if test = \"status!=null\" > AND u.status = #{status} </if>"
    		+ "<if test = \"departmentIds!=null and departmentIds.size()>0\" > "
	    		+ "AND g.department_id in "
	    		+ "<foreach collection=\"departmentIds\" index=\"index\" item=\"item\" open=\"(\" separator=\",\" close=\")\">#{item}</foreach>"
    		+ "</if>"
    		+ "<if test = \"createtime !=null and createtime.size()>1\" > "
    			+ " AND u.createtime BETWEEN  #{createtime[0]} and #{createtime[1]}"
    		+ "</if>"
    		+ "</where>"
    		//+ " order by u.id asc"
    		+ "</script>")
    List<WxUser> queryWithGroupIds(wxUserFilter filter);
    

}